package com.yeyuanqi.hottop.scheduletask;

import com.yeyuanqi.hottop.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 每月最火热搜定时任务
 * @author: yeyuanqi
 * @date: 2022/1/14
 */
@Component
public class MonthlyTopScheduleTask {

    private static final Logger logger = LoggerFactory.getLogger(DailyTopScheduleTask.class);

    @Autowired
    RedisService redisService;

    /**
     * 每天23点55分更新
     * 将本月top的最新热搜信息进行缓存管理
     */
    //百度每月top缓存更新
    @Scheduled(cron = "05 51 23 * * ?")
    public void bdMonthlyTOP(){
        //获取当前月份，年-月格式，作为zset的key
        String nowmonth = new SimpleDateFormat("yyyy-MM").format(new Date());
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        logger.info(now+"百度月top更新");
        String topdaily = now + "-baidudailytop";
        String topmonthly = nowmonth + "-baidumonthlytop";
        //返回当月top热搜信息
        long count = redisService.zCount(topmonthly);
        //获取当日top
        Map<Object, Double> dailytops = redisService.zAllScore(topdaily);
        //不存在相关缓存
        if (count==0){
            //以防万一，删除该zset的全部元素
            redisService.zDel(topmonthly);
            // 没有当月top热搜缓存，将当日top10热搜作为当日top热搜
            for ( Map.Entry<Object, Double> dailytop : dailytops.entrySet()) {
                redisService.zIncr(topmonthly,dailytop.getKey(),dailytop.getValue());
            }
        }else{
            //获取缓存中月top10热度最低
            Double monthlytopminscore = redisService.zMin(topmonthly);
            //比较热度，进行替换，zset无序返回，需要比较全部
            for ( Map.Entry<Object, Double> dailytop : dailytops.entrySet()){
                //热度比最小高，再比较缓存top10中是否已经存在该话题
                if (dailytop.getValue()>monthlytopminscore){
                    Double getscore= redisService.zScore(topmonthly, dailytop.getKey());
                    //缓存top不存在，则增加该话题热搜
                    if (getscore==null){
                        redisService.zAdd(topmonthly,dailytop.getKey(),dailytop.getValue());
                    } else if (getscore<dailytop.getValue()){
                        //缓存top10中已存在的话题热度小于当前最新热度，进行替换
                        redisService.zDel(topmonthly,dailytop.getKey());
                        redisService.zAdd(topmonthly,dailytop.getKey(),dailytop.getValue());
                    }
                }
            }
            //删除top10之后的热搜缓存
            redisService.zDelRange(topmonthly);
        }
    }

    //今日新闻每月top缓存更新
    @Scheduled(cron = "05 51 23 * * ?")
    public void jrMonthlyTOP(){
        //获取当前月份，年-月格式，作为zset的key
        String nowmonth = new SimpleDateFormat("yyyy-MM").format(new Date());
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        logger.info("今日头条月top更新");
        String topdaily = now + "-jinridailytop";
        String topmonthly = nowmonth + "-jinrimonthlytop";
        //返回当月top热搜信息
        long count = redisService.zCount(topmonthly);
        //获取当日top
        Map<Object, Double> dailytops = redisService.zAllScore(topdaily);
        //不存在相关缓存
        if (count==0){
            //以防万一，删除该zset的全部元素
            redisService.zDel(topmonthly);
            // 没有当月top热搜缓存，将当日top10热搜作为当日top热搜
            for ( Map.Entry<Object, Double> dailytop : dailytops.entrySet()) {
                redisService.zIncr(topmonthly,dailytop.getKey(),dailytop.getValue());
            }
        }else{
            //获取缓存中月top10热度最低
            Double monthlytopminscore = redisService.zMin(topmonthly);
            //比较热度，进行替换，zset无序返回，需要比较全部
            for ( Map.Entry<Object, Double> dailytop : dailytops.entrySet()){
                //热度比最小高，再比较缓存top10中是否已经存在该话题
                if (dailytop.getValue()>monthlytopminscore){
                    Double getscore= redisService.zScore(topmonthly, dailytop.getKey());
                    //缓存top不存在，则增加该话题热搜
                    if (getscore==null){
                        redisService.zAdd(topmonthly,dailytop.getKey(),dailytop.getValue());
                    } else if (getscore<dailytop.getValue()){
                        //缓存top10中已存在的话题热度小于当前最新热度，进行替换
                        redisService.zDel(topmonthly,dailytop.getKey());
                        redisService.zAdd(topmonthly,dailytop.getKey(),dailytop.getValue());
                    }
                }
            }
            //删除top10之后的热搜缓存
            redisService.zDelRange(topmonthly);
        }
    }

    //腾讯新闻每月top缓存更新
    @Scheduled(cron = "10 51 23 * * ?")
    public void txMonthlyTOP(){
        logger.info("腾讯新闻月top更新");
        //获取当前月份，年-月格式，作为zset的key
        String nowmonth = new SimpleDateFormat("yyyy-MM").format(new Date());
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String topdaily = now + "-tengxundailytop";
        String topmonthly = nowmonth + "-tengxunmonthlytop";
        //返回当月top热搜信息
        long count = redisService.zCount(topmonthly);
        //获取当日top
        Map<Object, Double> dailytops = redisService.zAllScore(topdaily);
        //不存在相关缓存
        if (count==0){
            //以防万一，删除该zset的全部元素
            redisService.zDel(topmonthly);
            // 没有当月top热搜缓存，将当日top10热搜作为当日top热搜
            for ( Map.Entry<Object, Double> dailytop : dailytops.entrySet()) {
                redisService.zIncr(topmonthly,dailytop.getKey(),dailytop.getValue());
            }
        }else{
            //获取缓存中月top10热度最低
            Double monthlytopminscore = redisService.zMin(topmonthly);
            //比较热度，进行替换，zset无序返回，需要比较全部
            for ( Map.Entry<Object, Double> dailytop : dailytops.entrySet()){
                //热度比最小高，再比较缓存top10中是否已经存在该话题
                if (dailytop.getValue()>monthlytopminscore){
                    Double getscore= redisService.zScore(topmonthly, dailytop.getKey());
                    //缓存top不存在，则增加该话题热搜
                    if (getscore==null){
                        redisService.zAdd(topmonthly,dailytop.getKey(),dailytop.getValue());
                    } else if (getscore<dailytop.getValue()){
                        //缓存top10中已存在的话题热度小于当前最新热度，进行替换
                        redisService.zDel(topmonthly,dailytop.getKey());
                        redisService.zAdd(topmonthly,dailytop.getKey(),dailytop.getValue());
                    }
                }
            }
            //删除top10之后的热搜缓存
            redisService.zDelRange(topmonthly);
        }
    }

    //微博每月top缓存更新
    @Scheduled(cron = "10 51 23 * * ?")
    public void wbMonthlyTOP(){
        logger.info("微博月top更新");
        //获取当前月份，年-月格式，作为zset的key
        String nowmonth = new SimpleDateFormat("yyyy-MM").format(new Date());
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String topdaily = now + "-weibodailytop";
        String topmonthly = nowmonth + "-weibomonthlytop";
        //返回当月top热搜信息
        long count = redisService.zCount(topmonthly);
        //获取当日top
        Map<Object, Double> dailytops = redisService.zAllScore(topdaily);
        //不存在相关缓存
        if (count==0){
            //以防万一，删除该zset的全部元素
            redisService.zDel(topmonthly);
            // 没有当月top热搜缓存，将当日top10热搜作为当日top热搜
            for ( Map.Entry<Object, Double> dailytop : dailytops.entrySet()) {
                redisService.zIncr(topmonthly,dailytop.getKey(),dailytop.getValue());
            }
        }else{
            //获取缓存中月top10热度最低
            Double monthlytopminscore = redisService.zMin(topmonthly);
            //比较热度，进行替换,zset无序返回，需要比较全部
            for ( Map.Entry<Object, Double> dailytop : dailytops.entrySet()){
                //热度比最小高，再比较缓存top10中是否已经存在该话题
                if (dailytop.getValue()>monthlytopminscore){
                    Double getscore= redisService.zScore(topmonthly, dailytop.getKey());
                    //缓存top不存在，则增加该话题热搜
                    if (getscore==null){
                        redisService.zAdd(topmonthly,dailytop.getKey(),dailytop.getValue());
                    } else if (getscore<dailytop.getValue()){
                        //缓存top10中已存在的话题热度小于当前最新热度，进行替换
                        redisService.zDel(topmonthly,dailytop.getKey());
                        redisService.zAdd(topmonthly,dailytop.getKey(),dailytop.getValue());
                    }

                }
            }
            //删除top10之后的热搜缓存
            redisService.zDelRange(topmonthly);
        }
    }
}
